package com.decent.ejfadmin.user.service.impl;

import cn.decent.component.shiro.service.AdminService;
import cn.decent.component.shiro.vo.AdminVO;
import com.decent.ejfadmin.common.bean.MessageBean;
import com.decent.ejfadmin.user.bean.AdminRole;
import com.decent.ejfadmin.user.bean.ShiroButton;
import com.decent.ejfadmin.user.dao.AdminButtonShiroDao;
import com.decent.ejfadmin.user.service.AdminButtonShiroService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * describe:按钮权限
 *
 * @author 陈豆豆
 * @date 2019/10/07
 */
@Slf4j
@Service
public class AdminButtonShiroServiceImpl implements AdminButtonShiroService {
    @Autowired
    private AdminButtonShiroDao adminButtonShiroDao;
    @Autowired
    private AdminService adminService;

    /**
     * 获取当前用户按钮权限
     *
     * @return
     */
    @Override
    public MessageBean getBtnShiro() {
        try {
            AdminVO currentAdmin = adminService.getCurrentAdmin();
            int num = adminButtonShiroDao.getBtnShiro(currentAdmin.getRoleId());
            if (num > 0) {
                return MessageBean.success();
            }
        } catch (Exception e) {
            log.warn("[获取按钮角色权限异常]{}", e, e);
        }
        return MessageBean.fail();
    }

    /**
     * 重新绑定按钮权限
     *
     * @param roleIdArr
     * @param buttonIdArr
     * @return
     */
    @Override
    public MessageBean rebindButton(Integer[] roleIdArr, Integer[] buttonIdArr) {
        try {
            adminButtonShiroDao.clearBtnBind(StringUtils.join(buttonIdArr, ","));
            for (Integer roleId : roleIdArr) {
                for (Integer buttonId : buttonIdArr) {
                    adminButtonShiroDao.rebindButton(roleId, buttonId);
                }
            }
            return MessageBean.success();
        } catch (Exception e) {
            log.warn("[重新绑定角色权限异常]{}", e, e);
            return MessageBean.fail("绑定失败");
        }
    }

    /**
     * 查询所有按钮
     *
     * @return
     */
    @Override
    public List<ShiroButton> getAllButton() {
        return adminButtonShiroDao.getAllButton();
    }

    /**
     * 获取所有角色
     *
     * @return
     */
    @Override
    public List<AdminRole> getAllRole() {
        List<AdminRole> adminRoleList = adminButtonShiroDao.getAllRole();
        adminRoleList.forEach(adminRole -> {
            List<String> btnName = adminButtonShiroDao.getBoundButton(adminRole.getId());
            adminRole.setBoundBtn(String.join(",", btnName));
        });
        return adminRoleList;
    }

    @Override
    public MessageBean getTransferApplyBtnShiro() {
        try {
            AdminVO currentAdmin = adminService.getCurrentAdmin();
            int num = adminButtonShiroDao.getTransferApplyBtn(currentAdmin.getRoleId());
            if (num > 0) {
                return MessageBean.success();
            }
        } catch (Exception e) {
            log.warn("[获取按钮角色权限异常]{}", e, e);
        }
        return MessageBean.fail();
    }

    /**
     * 查询已绑定角色
     *
     * @param bindRoleIds
     * @return
     */
    @Override
    public List<AdminRole> queryBoundRole(String bindRoleIds) {
        return adminButtonShiroDao.queryBindRoleNameByRoleId(bindRoleIds);
    }
}
